home *** CD-ROM | disk | FTP | other *** search
/ Aminet 25 / Aminet 25 (1998)(GTI - Schatztruhe)[!][Jun 1998].iso / Aminet / dev / debug / Wipeout.lha / source / addresstest.c next >
C/C++ Source or Header  |  1998-04-13  |  3KB  |  150 lines

  1. /*
  2.  * $Id: addresstest.c 1.6 1998/04/13 09:39:45 olsen Exp olsen $
  3.  *
  4.  * :ts=4
  5.  *
  6.  * Wipeout -- Traces and munges memory and detects memory trashing
  7.  *
  8.  * Written by Olaf `Olsen' Barthel <olsen@sourcery.han.de>
  9.  * Public Domain
  10.  */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "global.h"
  14. #endif    /* _GLOBAL_H */
  15.  
  16. /******************************************************************************/
  17.  
  18. #define NOT_IN_RAM (0)
  19.  
  20. /******************************************************************************/
  21.  
  22. #define ROM_SIZE_OFFSET 0x13
  23.  
  24. /******************************************************************************/
  25.  
  26. BOOL
  27. IsValidAddress(ULONG address)
  28. {
  29.     BOOL isValid = FALSE;
  30.  
  31.     /* Is this a valid RAM address? */
  32.     if(TypeOfMem((APTR)address) == NOT_IN_RAM)
  33.     {
  34.         extern LONG FAR romend;
  35.  
  36.         const ULONG romEnd        = (ULONG)&romend;
  37.         const ULONG romStart    = romEnd - (*(ULONG *)(romEnd - ROM_SIZE_OFFSET));
  38.  
  39.         /* Check if the address resides in ROM space. */
  40.         if(romStart <= address && address <= romEnd)
  41.         {
  42.             isValid = TRUE;
  43.         }
  44.     }
  45.     else
  46.     {
  47.         isValid = TRUE;
  48.     }
  49.  
  50.     /* In this context "valid" means that the data stored
  51.      * at the given address is safe to read.
  52.      */
  53.     return(isValid);
  54. }
  55.  
  56. /******************************************************************************/
  57.  
  58. BOOL
  59. IsInvalidAddress(ULONG address)
  60. {
  61.     BOOL isInvalid;
  62.  
  63.     isInvalid = (BOOL)(TypeOfMem((APTR)address) == NOT_IN_RAM);
  64.  
  65.     /* In this context "invalid" means that the data stored
  66.      * at the given address is not located in RAM, but
  67.      * somewhere else.
  68.      */
  69.     return(isInvalid);
  70. }
  71.  
  72. BOOL
  73. IsOddAddress(ULONG address)
  74. {
  75.     BOOL isOdd;
  76.  
  77.     isOdd = (BOOL)((address & 3) != 0);
  78.  
  79.     return(isOdd);
  80. }
  81.  
  82. /******************************************************************************/
  83.  
  84. BOOL
  85. IsAllocatedMemory(ULONG address,ULONG size)
  86. {
  87.     struct MemHeader * mh;
  88.     struct MemChunk * mc;
  89.     ULONG memStart;
  90.     ULONG memStop;
  91.     ULONG chunkStart;
  92.     ULONG chunkStop;
  93.     BOOL isAllocated = TRUE;
  94.  
  95.     /* check whether the allocated memory overlaps with free
  96.      * memory or whether freeing it would result in part of
  97.      * an already free area to be freed
  98.      */
  99.  
  100.     memStart    = address;
  101.     memStop        = address + size-1;
  102.  
  103.     Forbid();
  104.  
  105.     for(mh = (struct MemHeader *)SysBase->MemList.lh_Head ;
  106.         mh->mh_Node.ln_Succ != NULL ;
  107.         mh = (struct MemHeader *)mh->mh_Node.ln_Succ)
  108.     {
  109.         for(mc = mh->mh_First ;
  110.             mc != NULL ;
  111.             mc = mc->mc_Next)
  112.         {
  113.             chunkStart    = (ULONG)mc;
  114.             chunkStop    = chunkStart + mc->mc_Bytes-1;
  115.  
  116.             /* four cases are possible:
  117.              * 1) the chunk and the allocated memory do not overlap
  118.              * 2) the chunk and the allocated memory overlap at the beginning
  119.              * 3) the chunk and the allocated memory overlap at the end
  120.              * 4) the chunk and the allocated memory overlap completely
  121.              */
  122.  
  123.             if(memStop < chunkStart || memStart > chunkStop)
  124.             {
  125.                 /* harmless */
  126.             }
  127.             else if (memStart <= chunkStart && memStop <= chunkStop)
  128.             {
  129.                 isAllocated = FALSE;
  130.                 break;
  131.             }
  132.             else if (chunkStart <= memStart && chunkStop <= memStop)
  133.             {
  134.                 isAllocated = FALSE;
  135.                 break;
  136.             }
  137.             else if (  memStart <= chunkStart && chunkStop <= memStop ||
  138.                      chunkStart <= memStart   &&   memStop <= chunkStop)
  139.             {
  140.                 isAllocated = FALSE;
  141.                 break;
  142.             }
  143.         }
  144.     }
  145.  
  146.     Permit();
  147.  
  148.     return(isAllocated);
  149. }
  150.